package com.math_exercises;

public class MathExercises {
    public static void main(String[] args)
    {
        System.out.println("Given an Integer, if the number is 1 then print 'One', for 2 print 'Two', all the way up to 10. If the number is greater than 10 (or less than 1) print out 'Many'.");
        num_to_text(1);
        num_to_text(3);
        num_to_text(7);
        num_to_text(33);
        System.out.println("Given a number n, print the sum of all of the numbers from 1 to n.");
        System.out.println(sum_up_to_n(10));
        System.out.println("Given a number n, print n! (n factorial).");
        System.out.println(factorial(4));
        System.out.println("Given a number n, print the average of all of the numbers from 1 to n.");
        System.out.println(avg_up_to_n(10));
        System.out.println("Compute an approximation of pi using this formula:");
        System.out.println(pi_approximate(10000));
    }

    public static void num_to_text(int n)
    {
        switch(n){
            case 1: System.out.println("one");
                break;
            case 2: System.out.println("two");
                break;
            case 3: System.out.println("three");
                break;
            case 4: System.out.println("four");
                break;
            case 5: System.out.println("five");
                break;
            case 6: System.out.println("six");
                break;
            case 7: System.out.println("seven");
                break;
            case 8: System.out.println("eight");
                break;
            case 9: System.out.println("nine");
                break;
            case 10: System.out.println("ten");
                break;
            default: System.out.println("Sorry cannot translate");
                break;
        }
    }

    public static int sum_up_to_n(int n)
    {
        int sum=0;
        for (int x=1; x <= n; x++){
            sum = sum + x;
        }
        return sum;
    }

    public static int factorial(int n)
    {
        int factorial = 1;
        for (int x = 1; x <= n; x++){
            factorial = factorial * x;
        }
        return factorial;
    }

    public static double avg_up_to_n(int n)
    {
        return (double) sum_up_to_n(n)/n;
    }

    public static double pi_approximate(int n)
    {
        double pi;
        double multiplier=1.0;
        for (int x = 2; x <= n; x++ ){
            if(x % 2 == 0){
                multiplier = multiplier - (double) 1 / (x * 2 - 1);
            } else{
                multiplier = multiplier + (double) 1 / (x * 2 - 1);
            }
        }
        pi = 4 * multiplier;
        return pi;
    }
}

